package com.my.vote.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.my.vote.R;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.req.ReqSignUp;
import com.my.vote.pst.info.IPstSigUp;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.RxBusEventManager;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.IView;
import com.sfh.lib.mvp.service.LoaderService;
import com.sfh.lib.ui.AbstractActivity;
import com.sfh.lib.ui.DialogView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述:报名
 *
 * @author
 * @date 2018/4/14
 */
public class SignUpVoteActivity extends AbstractActivity implements IPstSigUp.IVSigUp {
    public  static class  SingUpToast{

    }
    public static void startUI(Context context, String voteId) {
        Intent intent = new Intent(context, SignUpVoteActivity.class);
        intent.putExtra("DATA", voteId);
        context.startActivity(intent);
    }

    @BindView(R.id.tvLeft)
    TextView tvLeft;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.etTitle)
    EditText etTitle;
    @BindView(R.id.etDes)
    EditText etDes;

    IPstSigUp pstSigUp;

    String voteId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_vote);
        ButterKnife.bind(this);
        tvLeft.setText("I want to sign up");
        tvRight.setText("SignUp");
        voteId = getIntent().getStringExtra("DATA");
    }

    @OnClick({R.id.tvLeft, R.id.tvRight})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvLeft:
                finish();
                break;
            case R.id.tvRight:
                ReqSignUp req = new ReqSignUp();
                req.setVoteId(voteId);
                req.setTitle(etTitle.getText().toString());
                req.setDesc(etDes.getText().toString());
                pstSigUp.sigup(req);
                break;
        }
    }

    @Override
    public void onSigUpSuccess() {

        // 报名成功提示对话框
        DialogView dialogView = new DialogView.Builder().setTitle("Prompt").setLeftText("Cancel").setRightText("OK")
                .setShowLeft(false).setMessage("Sigin up Success!").setCancele(false).setRightListener(new DialogView.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogFragment dialogFragment, int i) {
                RxBusEventManager.postEvent(new SingUpToast());
                dialogFragment.dismiss();
                finish();
            }
        }).build();
        showDialog(dialogView);
    }

    @Override
    public IPresenter getPresenter() {
        if (pstSigUp == null) {
            pstSigUp = LoaderService.getService(IPstSigUp.class);
        }
        return pstSigUp;
    }
}
